using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Application;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Util;

namespace Publicuse.Appication
{
    /// <summary>
    /// 日志管理
    /// </summary>
    public class LogApplication
    {
        private static BaesApplication baseApplication;
        private static LogApplication userApplication;

        public static LogApplication GetApplication(ServiceContainer container)
        {
            baseApplication = new BaesApplication(container);
            return userApplication;
        }

        static LogApplication()
        {
            userApplication = new LogApplication();
        }

        /// <summary>
        /// 获取系统所有模块
        /// </summary>
        /// <returns></returns>
        public async Task<CommonResult> GetList()
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                var table = TableAtributeUtil.GetTable<Menu>();
                var sql =
                    $"select aName from {table} where aType=2 and status=1 and ahtmlUrl<>'' and aName not like '%日志%';";
                var list = baseApplication.mySqlSugar.Query<Menu>(sql, out var msg);
                result.msg = msg;
                result.data = list;
                return result;
            });
        }

        /// <summary>
        /// 获取日志信息列表（功能操作统计）
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<CommonResult> GetLista(Log query)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                var tableName = TableAtributeUtil.GetTable<Log>();
                string sql = query.GetQueryString<Log>(null);
                var loginUserCode = HttpContextExtendBase.GetLoginCode(query.loginToken);
                var LoginAppid = HttpContextExtendBase.GetLoginAppid(query.loginToken);
                if (
                    string.Compare("admin", LoginAppid, true) != 0
                    && string.Compare(loginUserCode, "ajng", true) != 0
                )
                {
                    sql += $" and luser like '%{query.luser}%' ";
                }
                else
                {
                    if (!query.luser.IsNull())
                    {
                        sql += $" and luser like '%{query.luser}%' ";
                    }
                }
                sql += $" and cTime>= '{query.sttime}' and cTime<='{query.ettime}' ";
                LogQuery controllerexelogQuery = new LogQuery()
                {
                    tableName = tableName,
                    sqlWhere = sql,
                    sortFile = query.sortFile,
                    sortType = query.sortType,
                    pageIndex = query.pageIndex,
                    pageSize = query.pageSize,
                    files = "*"
                };
                long total = 0;
                List<Log> list = new List<Log>();
                baseApplication.mySqlSugar.GetList<Log>(
                    controllerexelogQuery,
                    out var msg,
                    out list,
                    out total
                );
                result.msg = msg;
                result.count = total;
                result.data = list;
                return result;
            });
        }

        /// <summary>
        /// 获取日志信息列表(控制器统计模式)
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<CommonResult> GetListb(Controllerexelog query)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                var tableName = TableAtributeUtil.GetTable<Controllerexelog>();
                string sql = query.GetQueryString<Controllerexelog>(null);
                var loginUserCode = HttpContextExtendBase.GetLoginCode(query.loginToken);
                var LoginAppid = HttpContextExtendBase.GetLoginAppid(query.loginToken);
                if (
                    string.Compare("admin", LoginAppid, true) != 0
                    && string.Compare(loginUserCode, "ajng", true) != 0
                )
                {
                    sql += $" and ucode like '%{query.ucode}%' ";
                }
                else
                {
                    if (!query.ucode.IsNull())
                    {
                        sql += $" and ucode like '%{query.ucode}%' ";
                    }
                }
                sql += $" and cTime>= '{query.sttime}' and cTime<='{query.ettime}' ";
                ControllerexelogQuery controllerexelogQuery = new ControllerexelogQuery()
                {
                    tableName = tableName,
                    sqlWhere = sql,
                    sortFile = query.sortFile,
                    sortType = query.sortType,
                    pageIndex = query.pageIndex,
                    pageSize = query.pageSize,
                    files = "*"
                };
                long total = 0;
                List<Controllerexelog> list = new List<Controllerexelog>();
                baseApplication.mySqlSugar.GetList<Controllerexelog>(
                    controllerexelogQuery,
                    out var msg,
                    out list,
                    out total
                );
                result.msg = msg;
                result.count = total;
                result.data = list;
                return result;
            });
        }

        /// <summary>
        ///   获取日志信息列表(登录日志统计模式)
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<CommonResult> GetListc(SigninInfo query)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                var tableName = TableAtributeUtil.GetTable<SigninInfo>();
                string sql = query.GetQueryString<SigninInfo>(null);
                var loginUserCode = HttpContextExtendBase.GetLoginCode(query.loginToken);
                var LoginAppid = HttpContextExtendBase.GetLoginAppid(query.loginToken);
                if (
                    string.Compare("admin", LoginAppid, true) != 0
                    && string.Compare(loginUserCode, "ajng", true) != 0
                )
                {
                    sql += $" and logincode like '%{query.logincode}%' ";
                }
                else
                {
                    if (!query.logincode.IsNull())
                    {
                        sql += $" and logincode like '%{query.logincode}%' ";
                    }
                }
                sql += $" and cTime>= '{query.sttime}' and cTime<='{query.ettime}' ";
                SigninInfoQuery signinInfoQuery = new SigninInfoQuery()
                {
                    tableName = tableName,
                    sqlWhere = sql,
                    sortFile = query.sortFile,
                    sortType = query.sortType,
                    pageIndex = query.pageIndex,
                    pageSize = query.pageSize,
                    files = "*"
                };
                long total = 0;
                List<SigninInfo> list = new List<SigninInfo>();
                baseApplication.mySqlSugar.GetList<SigninInfo>(
                    signinInfoQuery,
                    out var msg,
                    out list,
                    out total
                );
                result.msg = msg;
                result.count = total;
                result.data = list;
                return result;
            });
        }
    }
}
